<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function __autoload($class_name)
{
    require_once "../class/" . $class_name . ".class.php";
}

try {
    if (isset($_GET['id']) && $_GET['id'] > 0) {
        $int = $_GET['id'];
        $data = _getData($int);
        extract($data);
    } else {
        $dataN = _getDataOfMe();
        extract($dataN);
    }

    $_SESSION['ages'] = $ages;
    $_SESSION['contents'] = $contents;
    $_SESSION['image'] = $image;
    $_SESSION['alt_image'] = 'Xem tử vi năm Giáp ngọ 2014 - của ' . $ages;

} catch(FacebookApiException $e) {
    echo $e->getMessage() . '<br/>';
    $facebook->_error();
    exit;
}

function _getData($int) {
    $db = new DBHelper();
    $sql = "SELECT id, ages, contents, image FROM t_tuvi";
    $data = $db->execSQL($sql, array(), false);

    $dataN = array();;

    foreach ($data as $value) {
        if ($value['id'] == $int) {
            $dataN = $value;
            break;
        }
    }
    return $dataN;
}

function _getDataOfMe() {
    $db = new DBHelper();
    $sql = "SELECT id, ages, years, contents, image FROM t_tuvi";
    $data = $db->execSQL($sql, array(), false);
    
    $facebook = new FacebookApi();
    $user_profile = $facebook->_getMyProfile();
    $birthday = $user_profile['birthday'];
    $year_me = '';
    if (isset($birthday)) {
        $arrBirthday = explode("/", $birthday);
        $year_me = $arrBirthday[2];
    }
    
    foreach ($data as $value) {
        $arrYear = explode(", ", $value['years']);
        foreach ($arrYear as $year) {
            if ($year_me == $year) {
                $dataN = $value;
                break;
            }
        }
    }
    return $dataN;
}
?>